package com.onenine.app.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.onenine.app.R;
import com.onenine.app.db.DataManager;
import com.onenine.app.event.GameFilterEvent;
import com.onenine.app.model.GameCategory;
import com.onenine.app.utils.CDataProcessor;
import com.onenine.app.utils.CHelper;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.base.okhttp.OkRequest;
import com.onenine.base.utils.Toasty;
import com.onenine.base.view.NiceImageView;
import com.onenine.common.util.ApUtils;
import com.onenine.common.util.SizeUtils;
import com.onenine.glide.GlideApp;

import java.util.List;

/**
 * 游戏选项
 */
public class GameOptionView extends LinearLayout implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private LinearLayout mGameCategory, mGameMode;
    private RadioGroup mGamePlayerLimit;
    private GameSeekBar mCoinSeekBar;
    private View mvText, mvVoice, mvVideo;
    private NiceImageView mIvText, mIvVoice, mIvVideo;
    private List<GameCategory> mGameCategoryList;
    private int mBorderColor, mBorderWidth = 2;

    public GameOptionView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GameOptionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    private void initialize(Context context) {
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.layout_game_option_view, this, true);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int padding = SizeUtils.dp2px(20);
        setClipToPadding(false);
        setPadding(padding, padding, padding, padding);
        mGameCategory = findViewById(R.id.ll_category);
        mGameMode = findViewById(R.id.ll_mode);
        mGamePlayerLimit = findViewById(R.id.rg_sex);
        mCoinSeekBar = findViewById(R.id.gsb_bar);
        mvText = findViewById(R.id.inc_text);
        mvVoice = findViewById(R.id.inc_voice);
        mvVideo = findViewById(R.id.inc_video);
        mBorderColor = RoleResLoader.getRoleColorResourceId(RoleResLoader.DynamicResName.THEME_COLOR);
        setSexLimit();
        mGamePlayerLimit.setOnCheckedChangeListener(this);
        for (int i = 0; i < mGamePlayerLimit.getChildCount(); i++) {
            mGamePlayerLimit.getChildAt(i).setBackgroundResource(RoleResLoader
                    .getRoleDrawableResourceId(RoleResLoader.DynamicResName.SELECTOR_GAME_BUTTON_BG));
        }
    }

    private void setSexLimit() {
        mvText.setOnClickListener(this);
        mvVoice.setOnClickListener(this);
        mvVideo.setOnClickListener(this);
        TextView tvText = mvText.findViewById(R.id.tv_text);
        tvText.setText("文字");
        mIvText = mvText.findViewById(R.id.iv_img);
        mIvText.setImageResource(R.drawable.ic_game_text_mode_big);
        mIvText.setBorderColor(mBorderColor);
        TextView tvVoice = mvVoice.findViewById(R.id.tv_text);
        tvVoice.setText("语音");
        mIvVoice = mvVoice.findViewById(R.id.iv_img);
        mIvVoice.setImageResource(R.drawable.ic_game_voice_mode_big);
        mIvVoice.setBorderColor(mBorderColor);
        TextView tvVideo = mvVideo.findViewById(R.id.tv_text);
        tvVideo.setText("视频");
        mIvVideo = mvVideo.findViewById(R.id.iv_img);
        mIvVideo.setImageResource(R.drawable.ic_game_video_mode_big);
        mIvVideo.setBorderColor(mBorderColor);
        //fixme 先隐藏视频
        mvVideo.setVisibility(View.GONE);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        getGameCategoryData();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        OkRequest.getInstance().cancelTag(GameOptionView.class.getSimpleName());
    }

    /**
     * 获取游戏种类数据
     */
    private void getGameCategoryData() {
        mGameCategoryList = DataManager.getAllGameCategorys();
        if (mGameCategoryList == null || mGameCategoryList.isEmpty()) {
            CDataProcessor.syncGameCategory(new Runnable() {
                @Override
                public void run() {
                    mGameCategoryList = DataManager.getAllGameCategorys();
                    showGameCategoryList();
                }
            });
        } else {
            showGameCategoryList();
        }
    }

    /**
     * 显示游戏种类
     */
    private void showGameCategoryList() {
        int iconSize = getResources().getDimensionPixelSize(R.dimen.game_item_icon_size);
        mGameCategory.removeAllViews();
        for (GameCategory gameCategory : mGameCategoryList) {
            View contentView = LayoutInflater.from(ApUtils.getApp())
                    .inflate(R.layout.layout_game_option_item, null, true);
            mGameCategory.addView(contentView);
            NiceImageView ivImg = contentView.findViewById(R.id.iv_img);
            ivImg.setBorderColor(mBorderColor);
            GlideApp.with(ApUtils.getApp()).load(CHelper.getOssThumbImageUrl(gameCategory.getGameIcon(), iconSize)).into(ivImg);
            TextView tvText = contentView.findViewById(R.id.tv_text);
            tvText.setText(gameCategory.getGameName());
            contentView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    contentView.setSelected(true);
                    ivImg.setBorderWidth(mBorderWidth);
                    mGameCategory.setTag(gameCategory);
                    for (int i = 0; i < mGameCategory.getChildCount(); i++) {
                        View vc = mGameCategory.getChildAt(i);
                        if (vc != contentView) {
                            vc.setSelected(false);
                            ((NiceImageView) (vc.findViewById(R.id.iv_img)))
                                    .setBorderWidth(0);
                        }
                    }
                }
            });
        }
    }

    /**
     * 选项是否已选择
     *
     * @return
     */
    public boolean verifyOptionSelection() {
        if (mGameCategory.getTag() == null) {
            Toasty.showNormal(R.string.game_category_hint);
            return false;
        }
        if (mGameMode.getTag() == null) {
            Toasty.showNormal(R.string.game_mode_hint);
            return false;
        }
        if (mGamePlayerLimit.getTag() == null) {
            Toasty.showNormal(R.string.game_player_limit_hint);
            return false;
        }
        return true;
    }

    /**
     * 设置上次数据
     *
     * @param event
     */
    public void setGameFilterEvent(GameFilterEvent event) {
//        if (event != null) {
//            if (event.gameCategory != null) {
//                mGameCategory.setRightText(event.gameCategory.getGameName());
//                mGameCategory.setRightTextTag(event.gameCategory);
//            }
//            mGameBetAmount.setRightText(event.betAmountPair.first);
//            mGameBetAmount.setRightTextTag(event.betAmountPair.second);
//            mGamePlayerLimit.setRightText(event.limitPair.first);
//            mGamePlayerLimit.setRightTextTag(event.limitPair.second);
//            mGameMode.setRightText(event.modePair.first);
//            mGameMode.setRightTextTag(event.modePair.second);
//        } else {
//            mGameCategory.setRightText("");
//            mGameCategory.setRightTextTag(null);
//            mGameBetAmount.setRightText("");
//            mGameBetAmount.setRightTextTag(null);
//            mGamePlayerLimit.setRightText("");
//            mGamePlayerLimit.setRightTextTag(null);
//            mGameMode.setRightText("");
//            mGameMode.setRightTextTag(null);
//        }
    }

    /**
     * 获取游戏种类
     *
     * @return
     */
    public GameCategory getGameCategory() {
        return (GameCategory) mGameCategory.getTag();
    }

    /**
     * 获取游戏模式
     *
     * @return
     */
    public Pair<String, Integer> getGameModePair() {
        Object object = mGameMode.getTag();
        if (object != null) {
            try {
                return Pair.create("game_mode", Integer.parseInt(object.toString()));
            } catch (NumberFormatException e) {
            }
        }
        return Pair.create("game_mode", 0);
    }

    /**
     * 获取游戏玩家限制
     *
     * @return
     */
    public Pair<String, Integer> getGamePlayerLimitPair() {
        Object object = mGamePlayerLimit.getTag();
        if (object != null) {
            try {
                return Pair.create("game_limit", Integer.parseInt(object.toString()));
            } catch (NumberFormatException e) {
            }
        }
        return Pair.create("game_limit", 0);
    }

    /**
     * 获取游戏赌注额
     *
     * @return
     */
    public Pair<String, Integer> getGameBetAmountPair() {
        return Pair.create("game_amount", mCoinSeekBar.getSelectValue());
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (group.getId() == mGamePlayerLimit.getId()) {
            for (int i = 0; i < mGamePlayerLimit.getChildCount(); i++) {
                View view = mGamePlayerLimit.getChildAt(i);
                if (checkedId == view.getId()) {
                    mGamePlayerLimit.setTag(view.getTag());
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.inc_text:
                mGameMode.setTag(1);
                mvText.setSelected(true);
                mIvText.setBorderWidth(mBorderWidth);
                mvVoice.setSelected(false);
                mIvVoice.setBorderWidth(0);
                mvVideo.setSelected(false);
                mIvVideo.setBorderWidth(0);
                break;
            case R.id.inc_voice:
                mGameMode.setTag(2);
                mvText.setSelected(false);
                mIvText.setBorderWidth(0);
                mvVoice.setSelected(true);
                mIvVoice.setBorderWidth(mBorderWidth);
                mvVideo.setSelected(false);
                mIvVideo.setBorderWidth(0);
                break;
            case R.id.inc_video:
                mGameMode.setTag(3);
                mvText.setSelected(false);
                mIvText.setBorderWidth(0);
                mvVoice.setSelected(false);
                mIvVoice.setBorderWidth(0);
                mvVideo.setSelected(true);
                mIvVideo.setBorderWidth(mBorderWidth);
                break;
        }
    }

}
